﻿#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QTimer>
#include <stdio.h>
#include <stdlib.h>
#include <QQmlContext>
#include <QObject>
#include <QDebug>
#include <QFile>

#define HS 20
#define LS 60
#define SHI 1
#define FOU 0
#define XB '1'
#define KB '0'
#define HUO1 2
#define HUO2 3

int sf_jx(void);    //是否继续
void du_cssj(char [][LS], const int);   //读入初始数据
void xs_shijie(char [][LS], const int); //显示世界
void js_weilai(char [][LS], const int); //计算未来世界
void cb_shijie(char [][LS], char [][LS], const int); //拷贝数组from到数组to
int js_ljsm(const int, const int, char [][LS], const int);  //计算邻居数目

static QObject* myQml;
static QQmlApplicationEngine *gengine;
static char shijie[HS][LS];
static bool b=true;
struct position
{
    position() {}
    int h;
    int l;
    char value;
};

static struct position pos;

//int main()
//{
//    char shijie[HS][LS];

//    du_cssj(shijie, sizeof(shijie)/sizeof(shijie[0]));

//    do{
//        xs_shijie(shijie, sizeof(shijie)/sizeof(shijie[0]));
//        js_weilai(shijie, sizeof(shijie)/sizeof(shijie[0]));
//    }while(sf_jx());

//    system("PAUSE");
//    return 0;
//}
/*
 * 计算邻居数目
 * gzh:格子行
 * gzl:格子列
 * sj:世界
 * sjhs:世界行数
*/
int js_ljsm(const int gzh, const int gzl, char sj[][LS], const int sjhs)
{
    int sm = 0;
    int h,l;
    for(h=gzh-1;h<=gzh+1;h++)
    {
        for(l=gzl-1;l<=gzl+1;l++)
        {
            if(h<0 || h>=sjhs)
                continue;
            if(l<0 || l>(sizeof(sj[0]) / sizeof(sj[0][0])))
                continue;
            if(h==gzh && l==gzl)
                continue;
            if(sj[h][l] == XB)
                sm++;
        }
    }
    return sm;
}
/*
 * 拷贝数组from到数组to
 * hs:行数
*/
void cb_shijie(char from[][LS], char to[][LS], const int hs)
{
    int h,l;
    for(h=0;h<hs;h++)
    {
        for(l=0;l<sizeof(from[0])/sizeof(from[0][0]);l++)
        {
            to[h][l] = from[h][l];
        }
    }
    return;
}
/*
sj:世界数组
hs:行数
*/
void js_weilai(char sj[][LS], const int hs)
{
    char dqsj[HS][LS];  //当前世界

    cb_shijie(sj, dqsj, hs);

    {
        int h,l;
        for(h=0;h<hs;h++)
        {
            for(l=0;l<sizeof(sj[0])/sizeof(sj[0][0]);l++)
            {
                int ljsm;   //邻居数目
                ljsm = js_ljsm(h,l,dqsj,sizeof(dqsj)/sizeof(dqsj[0]));
                switch (ljsm) {
                case HUO1:
                    //格子不变
                    break;
                case HUO2:
                    //格子必定是细胞
                    sj[h][l] = XB;
                    break;
                default:
                    //格子是空格
                    sj[h][l] = KB;
                    break;
                }
            }
        }
    }

    return;
}
/*
sj:世界数组
hs:行数
*/
void xs_shijie(char sj[][LS], const int hs)
{
    int h,l;
//    system("CLS");
    for(h=0;h<hs;h++)
    {
        for(l=0;l<sizeof(sj[0])/sizeof(sj[0][0]);l++)
        {
            gengine->rootContext()->setContextProperty("hang", h);
            gengine->rootContext()->setContextProperty("lie", l);
            gengine->rootContext()->setContextProperty("value1", sj[h][l]);
            gengine->rootContext()->setContextProperty("flag", b=!b);

        }
//        putchar('\n');
    }
//    putchar('\n');
//    putchar('\n');

    return;
}

/*
sj:世界数组
hs:行数
*/
void du_cssj(char sj[][LS], const int hs)
{
    int h,l;

    freopen("D:\\data.txt", "r", stdin);

    for(h=0;h<hs;h++)
    {
        for(l=0;l<sizeof(sj[0])/sizeof(sj[0][0]);l++)
        {
            char c;
            scanf("%c", &c);
#define XBBZ '*'
            sj[h][l] = (c==XBBZ)?XB:KB;
#undef XBBZ
        }
        getchar();
    }

    freopen("CON","r",stdin);

    return;
}
int sf_jx(void)
{
//    printf("是否继续？（回车继续，其他停止）\n");
    printf("is continue?(enter or other)\n");
    return (getchar() == '\n')?SHI:FOU;
}



class One: public QObject
{
//    Q_OBJECT
public:
    One()
    {
        QTimer * timer = new QTimer();
        QObject::connect(timer, &QTimer::timeout, this, &One::calc);
        timer->start(100);
    }
    ~One(){}

public slots:
    void calc()
    {
        xs_shijie(shijie, sizeof(shijie)/sizeof(shijie[0]));
        js_weilai(shijie, sizeof(shijie)/sizeof(shijie[0]));

//        QMetaObject::invokeMethod(myQml,"setStatus",Qt::DirectConnection,Q_ARG(QVariant,QVariant::fromValue(p.h)),
//                                  Q_ARG(QVariant,QVariant::fromValue(p.l)),
//                                  Q_ARG(QVariant,QVariant::fromValue(p.value)));


//        if(myQml)
//        {
//            QMetaObject::invokeMethod(myQml,"setStatus",Qt::DirectConnection,Q_ARG(QVariant,QVariant::fromValue(p.h)),
//                                      Q_ARG(QVariant,QVariant::fromValue(p.l)),
//                                      Q_ARG(QVariant,QVariant::fromValue(p.value)));
//        }
//        else{
//            qDebug()<<"===myQml none";

//            myQml = gengine->rootContext()->findChild<QObject *>("myQml");
//        }
    }
};

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    du_cssj(shijie, sizeof(shijie)/sizeof(shijie[0]));


    QQmlApplicationEngine engine;

    gengine = &engine;

    gengine->rootContext()->setContextProperty("hang", 0);
    gengine->rootContext()->setContextProperty("lie", 0);
    gengine->rootContext()->setContextProperty("value1", 0);
    gengine->rootContext()->setContextProperty("flag", b=!b);

    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;

    One one;

    return app.exec();
}
